#include "log.h"


static TraceFile  g_trackefile;

void GetExePath(char* buf)
{
	char filepath[10240];

	getcwd(filepath, 10240); 
    sprintf(buf,"%s",filepath);
	printf("buf = %s\r\n",buf);
}


TraceFile::TraceFile()
{
	m_process = getpid();
	
	GetExePath(m_szlog_dir);
	printf("m_szlog_dir =%s \r\n",m_szlog_dir);
    sprintf(m_szlog_file, "%s/%d.log", m_szlog_dir, m_process);
	m_fslog.open(m_szlog_file);
	printf("m_szlog_file =%s \r\n",m_szlog_file);
}

TraceFile::~TraceFile()
{
  m_fslog.close();
}

void   TraceFile::WriteTrace(char* levelname,const char* str)
{	
	func_lock l_lock;
	if(str == NULL)
		return;
		
	char   strTime[50];
        struct tm tmVar;
        struct timeval  timeVal;	
  unsigned int dwTimeLen = sprintf(strTime, "<thread_Id=%u>",timeVal.tv_usec / 1000,
	syscall(SYS_gettid));
	
    m_fslog << strTime ;
	m_fslog <<"level="<< levelname;
	m_fslog <<",log=" << str; 
	m_fslog <<"\r\n";
	
	Flush();
	
}
void   TraceFile::Flush()
{
	m_fslog.flush();
}

char*  TraceFile::GetLogFile()
{
	return m_szlog_file;
}

logfile::logfile(char* levelname)
{
    uint32 lnsize = strlen(levelname);
    
	memset(m_levelname,0,32);
	strcpy(m_levelname,levelname);
}

logfile::~logfile()
{
}

void logfile::WriteTrace(const char* str)
{
	if(str == NULL)
		return;
	
	g_trackefile.WriteTrace(m_levelname,str);
}

